<?php

namespace StudyBuddy;

/**
 * Class responsible for
 * increasing/decreasing count
 * of tags in UNANSWERED_TAGS collection
 *
 * @todo this can be in Registry as singleton
 */
class UnansweredTags extends StudyBuddyObject {

    /**
     * Mongo collection UNANSWERED_TAGS
     *
     * @var object of type MongoCollection representing UNANSWERED_TAGS
     */
    protected $coll;

    public function __construct(Registry $oRegistry) {
        $this->oRegistry = $oRegistry;
        $this->coll = $oRegistry->Mongo->UNANSWERED_TAGS;

        $this->coll->ensureIndex(array('tag' => 1), array('unique' => true));
        $this->coll->ensureIndex(array('i_ts' => 1));
    }

    /**
     * Increases count for each tag
     * in tags from supplied question
     *
     * @param object $oQuestion
     */
    public function set(Question $oQuestion) {
        $aTags = $oQuestion->offsetGet('a_tags');


        if (!is_array($aTags) || empty($aTags)) {

            return $this;
        }

        foreach ($aTags as $tag) {
            try {

                $this->coll->update(array("tag" => $tag), array('$inc' => array("i_count" => 1), '$set' => array('i_ts' => time(), 'hts' => date('F j, Y, g:i a T'))), array("upsert" => true));
            } catch (\MongoException $e) {
                //e('unable to upsert UNANSWERED_TAGS: '.$e->getMessage());
            }
        }
    }

    /**
     * Decrseases count of tag for given question
     * or completely removes that tag if count
     * is already at 1 because it does not make
     * sense to keep tag with a count of 0
     *
     * Every time a question is set as answered
     * this object/method should be invoked and pass
     * the question object to it
     *
     * @param Question $oQuestion
     */
    public function remove($oQuestion) {

        if (!is_array($oQuestion) && (!($oQuestion instanceof \StudyBuddy\Question))) {
            throw new \InvalidArgumentException('$oQuestion must be array OR instance of Question. was: ' . gettype($oQuestion));
        }

        $aTags = (is_array($oQuestion)) ? $oQuestion : $oQuestion['a_tags'];


        if (empty($aTags) || !is_array($aTags)) {

            return;
        }

        /**
         * If tag exists in collection and > 0 then decrsease count,
         * otherwise remove it
         *
         */
        foreach ($aTags as $tag) {
            $aItem = $this->coll->findOne(array('tag' => $tag));
            if (null === $aItem) {
                //d('No record for tag: '.$tag.' in UNANSWERED_TAGS');
                continue;
            }

            if (1 === $aItem['i_count']) {
                //d('removing tag: '.$tag.' from UNANSWERED_TAGS');
                $this->coll->remove(array('tag' => $tag));
            } else {
                //d('decreasing count for tag: '.$tag.' in UNANSWERED_TAGS');
                try {
                    $this->coll->update(array("tag" => $tag), array('$inc' => array("i_count" => -1)));
                } catch (\MongoException $e) {
                    //e('unable to update UNANSWERED_TAGS collection: '.$e->getMessage());
                }
            }
        }

        /**
         * Post onAcceptedAnswer
         * the tagsUnanswered have to be unset from cache
         * as well as posssibly some other cached items
         */
        $this->oRegistry->Dispatcher->post($this, 'onRemovedUnansweredTags', $aTags);
    }

}
